package com.wfg.common;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wufagang
 * @description
 * @date 2023年05月10日 07:43
 */
public class RemoteMapRegister {
    private static Map<String, List<Url>> REGISTER = new HashMap<>();

    public static void regist(String interfaceName, Url url){
//        REGISTER.putIfAbsent(interfaceName, new ArrayList<>());
        List<Url> urls = REGISTER.get(interfaceName);
        if(urls == null){
            List<Url> objects = new ArrayList<>();
            objects.add(url);
            REGISTER.put(interfaceName, objects);
        }else {
            urls.add(url);
        }
        saveFile();
    }
    public static List<Url> get(String interfaceName){
        REGISTER = getFile();
        return REGISTER.get(interfaceName);
    }

    private static Map<String, List<Url>> getFile() {
        try {
            FileInputStream inputStream = new FileInputStream("classpath:register.txt");
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
             return (Map<String, List<Url>>)objectInputStream.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
        return null;
    }

    private static void saveFile() {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("classpath:register.txt");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(REGISTER);
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
    }
}
